<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.css" rel="stylesheet">
  <style>
    .ani1-enter{
      transform: translateX(-500px);
      opacity: 0;
    }
    .ani1-enter-active,.ani1-leave-active{
      transition: all 400ms;
    }
    .ani1-leave-to{
      transform: translateX(500px);
      opacity: 0;
    }
  </style>
</head>
<body>
  <div id="app">
    <div class="wrap" style="max-width:600px;margin:20px auto">
      <h2>今日事、今日毕</h2>
      <div class="row">
        <div class="col-md-10 col-sm-10">
          <input type="text" v-model="inputTxt" class="form-control">
        </div>
        <div class="col-md-2 col-sm-2">
          <button @click="addTodo" class="btn btn-primary">增加</button>
        </div>
      </div>
      <div class="content" style="margin-top: 10px">
        <ul class="list-group">
          <li v-if="!todos.length" class="text-center list-group-item">暂无代办事项</li>
         <transition-group name="ani1">
            <li 
            v-for="(todo,index) in todos"
            :key="index"
            class="list-group-item">
            {{ todo.content }}
            <button 
              @click="delTodo(index)"
              class="btn btn-danger btn-xs">删除</button>
            <button 
              :disabled="todo.isCompleted" class="btn btn-success btn-xs">{{ todo.isCompleted?'已完成':'点击完成' }}</button>
          </li>
         </transition-group>
        </ul>
      </div>
    </div>
  </div>
  <script src="./vue.js"></script>
  <script>
    const vm = new Vue({
      el: '#app',
      data: {
        inputTxt:'',
        todos:[
         {
           content:'明天爬山',
           isCompleted: false
         },
         {
           content:'明天爬1111',
           isCompleted: true
         }
        ]
      },
      methods: {
        delTodo(index){
          // 删除代办事项
          if(confirm('您确认删除吗?')){
            this.todos.splice(index,1)
          }
        },
        addTodo(){
          // 添加代办事项
          if(this.inputTxt.trim() === ""){
            return 
          }
          this.todos.push({
            content: this.inputTxt,
            isCompleted: false
          })
          this.inputTxt = "";
        }
      }
    })
  </script>
</body>
</html>